package lab.bmp.studio;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import lab.bmp.NamedRecordBean;
import lab.bmp.track.TrackPK;
import lab.domain.Track;
import lab.logging.LoggingUtil;
import ru.ssau.DatabaseHelper;

/**
 *
 * @author Smike
 */
public class StudioBean implements NamedRecordBean<StudioPK> {

    EntityContext context;
    DataSource datasource;
    lab.domain.SoundRecordingStudio studio;
    boolean changed;

    public void setEntityContext(EntityContext ctx) throws EJBException, RemoteException {
        this.context = ctx;
    }

    public void unsetEntityContext() throws EJBException, RemoteException {
        this.context = null;
    }

    public void ejbRemove() throws RemoveException, EJBException, RemoteException {
        StudioPK key = getCurrentKey();
        lab.domain.SoundRecordingStudio studio = new lab.domain.SoundRecordingStudio();
        studio.setId(key.getId());
        Connection connection = getConnection();
        try {
            DatabaseHelper.deleteStudio(connection, studio);
        } catch (SQLException ex) {
            throw new EJBException(ex.getMessage());
        } finally {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }

    public void ejbActivate() throws EJBException, RemoteException {
        // do nothing
    }

    public void ejbPassivate() throws EJBException, RemoteException {
    }

    public void ejbLoad() throws EJBException, RemoteException {
        changed = false;
        // lazy
    }

    private void initializeIfNeeded() {
        if (studio == null) {
            StudioPK key = getCurrentKey();
            Connection connection = getConnection();
            try {
                this.studio = DatabaseHelper.findStudio(connection, key.getId());
                LoggingUtil.log(LoggingUtil.STUDIO);
            } catch (SQLException ex) {
                throw new EJBException(ex.getMessage());
            } finally {
                try {
                    connection.close();
                } catch (Exception e) {
                }
            }
        }
    }

    public void ejbStore() throws EJBException, RemoteException {
        if (studio != null && changed) {
            Connection connection = getConnection();
            try {
                DatabaseHelper.updateStudio(connection, studio);
            } catch (SQLException ex) {
                throw new EJBException(ex.getMessage());
            } finally {
                try {
                    connection.close();
                } catch (Exception e) {
                }
            }
        }
    }

    StudioPK getCurrentKey() {
        return (StudioPK) context.getPrimaryKey();
    }

    public String getName() {
        initializeIfNeeded();
        return studio.getName();
    }

    public void setName(String name) {
        initializeIfNeeded();
        studio.setName(name);
        changed = true;
    }

    public Integer getId() {
        return getCurrentKey().getId();
    }

    public void setId(Integer id) {
        initializeIfNeeded();
        studio.setId(id);
        changed = true;
    }

    public StudioPK ejbCreate(Integer id, String name) throws CreateException {
        lab.domain.SoundRecordingStudio studio = new lab.domain.SoundRecordingStudio();
        studio.setName(name);
        Connection connection = getConnection();
        try {
            return new StudioPK(DatabaseHelper.insertStudio(connection, studio));
        } catch (SQLException ex) {
            throw new CreateException(ex.getMessage());
        } finally {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }

    }

    public StudioPK ejbFindByPrimaryKey(StudioPK key) throws FinderException {
        lab.domain.SoundRecordingStudio studio;
        Connection connection = getConnection();
        try {
            studio = DatabaseHelper.findStudio(connection, key.getId());
            return key;
        } catch (SQLException ex) {
            throw new FinderException(ex.getMessage());
        } finally {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }

    public Collection ejbFindAll() throws FinderException {
        Connection connection = getConnection();
        try {
            Collection<lab.domain.SoundRecordingStudio> Studios = DatabaseHelper.findStudios(connection);
            Collection<StudioPK> keys = new ArrayList<StudioPK>();
            for (lab.domain.SoundRecordingStudio Studio : Studios) {
                keys.add(new StudioPK(Studio.getId()));
            }
            return keys;
        } catch (SQLException ex) {
            throw new FinderException(ex.getMessage());
        } finally {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }

    public StudioPK ejbFindByTrack(TrackPK key) throws FinderException {
        Connection connection = getConnection();
        try {
            Track track = DatabaseHelper.findTrack(connection, key.getId());
            return new StudioPK(track.getSoundRecordingStudio().getId());
        } catch (SQLException ex) {
            throw new FinderException(ex.getMessage());
        } finally {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }

    }

    Connection getConnection() {
        try {
            return getDataSource().getConnection();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

    DataSource getDataSource() {
        if (datasource == null) {
            try {
                datasource = (DataSource) new InitialContext().lookup("oracleDS");
                return datasource;
            } catch (NamingException ex) {
                throw new RuntimeException(ex);
            }
        }
        return datasource;
    }
}
